package com.sky.controller.admin;

import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/admin/common/upload")
@Slf4j
@Api(tags = "文件上传接口")
public class FileController {

    @ApiOperation("文件上传接口")
    @PostMapping
    public Result upload(@RequestParam("file") MultipartFile file) throws IOException {

        //获取文件名
        String name = file.getOriginalFilename();
        //获取"."后面的文件格式是在第几个字符后面

        int i = 0;
        if (name != null) {
            i = name.lastIndexOf(".");
        }

        //获取文件格式
        String suffix = null;
        if (name != null) {
            suffix = name.substring(i);
        }
        //将uuid与文件格式名拼接 作为新的文件名
        String newFileName = UUID.randomUUID() + suffix;

        //上传到本地磁盘目录，将重新拼接的文件名作为命名
        file.transferTo(new File("E:\\Java\\苍穹外卖\\file\\"+newFileName));
        //E:\JAVA\苍穹外卖\file

        return Result.success("E:\\JAVA\\苍穹外卖\\file\\"+newFileName);
    }

}
